<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键词" prop="searchtxt">
        <el-input
          v-model="queryParams.searchtxt"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:activity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:activity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:activity:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="账号" align="center" prop="enterpriseContactPhone" width="120" />
      <el-table-column label="企业名称" align="center" prop="enterpriseFullname" />
      <el-table-column label="地区" align="center" prop="enterpriseAddress" />
      <el-table-column label="提交时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="60">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row, 'status')"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
          >审核通过</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            class="text-red"
            icon="el-icon-remove-outline"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="会员级别" prop="memberLevel">
          <el-select v-model="form.memberLevel" placeholder="请选择">
            <!-- <el-option v-for="item in levelList" :key="item.id" :label="item.level" :value="item.id"></el-option> -->
             <el-option label="免费账户" :value="0"></el-option>
             <el-option label="基础账户" :value="1"></el-option>
             <el-option label="标准账户" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业全称" prop="enterpriseFullname">
          <el-input v-model="form.enterpriseFullname" placeholder="请输入企业全称" />
        </el-form-item>
        <el-form-item label="所属行业" prop="enterpriseIndustry">
          <el-radio-group class="more-radio-group" v-model="form.enterpriseIndustry">
            <el-radio label="汽车"></el-radio>
            <el-radio label="生命科学"></el-radio>
            <el-radio label="TMT"></el-radio>
            <el-radio label="金融"></el-radio>
            <el-radio label="消费与零售"></el-radio>
            <el-radio label="能源与交通"></el-radio>
            <el-radio label="建筑工程房地产"></el-radio>
            <el-radio label="其他"></el-radio>
          </el-radio-group>
          <el-input class="other-input" v-show="form.enterpriseIndustry == '其他'" v-model="form.enterpriseIndustryMore" placeholder="请输入其他行业领域" />
        </el-form-item>

        <el-form-item label="企业类型" prop="enterpriseScale">
          <el-select v-model="form.enterpriseScale" placeholder="请选择">
            <!-- <el-option v-for="item in levelList" :key="item.id" :label="item.level" :value="item.id"></el-option> -->
            <el-option :value="11" label="世界500强集团总部"></el-option>
            <el-option :value="12" label="世界500强子公司"></el-option>
            <el-option :value="13" label="中国500强集团总部"></el-option>
            <el-option :value="14" label="中国500强子公司"></el-option>
            <el-option :value="1" label="世界500强企业"></el-option>
            <el-option :value="2" label="中国500强企业"></el-option>
            <el-option :value="4" label="上市公司"></el-option>
            <el-option :value="8" label="独角兽"></el-option>
            <el-option :value="9" label="行业前列"></el-option>
            <el-option :value="5" label="其他大中企业"></el-option>
            <el-option :value="6" label="中小企业"></el-option>
            <el-option :value="7" label="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公司性质" prop="enterpriseType">
          <el-select v-model="form.enterpriseType" placeholder="请选择">
            <!-- <el-option v-for="item in levelList" :key="item.id" :label="item.level" :value="item.id"></el-option> -->
            <el-option :value="3" label="外企"></el-option>
            <el-option :value="1" label="央企/国企"></el-option>
            <el-option :value="2" label="民企"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="所在地区" prop="enterpriseCitys">
          <el-select v-model="form.enterpriseCitys" placeholder="请选择">
            <!-- <el-option v-for="item in levelList" :key="item.id" :label="item.level" :value="item.id"></el-option> -->
            <el-option :value="1" label=北京></el-option>
            <el-option :value="2" label=上海></el-option>
            <el-option :value="3" label=广州></el-option>
            <el-option :value="9" label=深圳></el-option>
            <el-option :value="26" label=浙江></el-option>
            <el-option :value="22" label=湖南></el-option>
            <el-option :value="21" label=青海></el-option>
            <el-option :value="24" label=江西></el-option>
            <el-option :value="25" label=安徽></el-option>
            <el-option :value="23" label=湖北></el-option>
            <el-option :value="27" label=福建></el-option>
            <el-option :value="28" label=广东></el-option>
            <el-option :value="29" label=贵州></el-option>
            <el-option :value="30" label=云南></el-option>
            <el-option :value="31" label=海南></el-option>
            <el-option :value="20" label=四川></el-option>
            <el-option :value="19" label=甘肃></el-option>
            <el-option :value="18" label=陕西></el-option>
            <el-option :value="10" label=吉林></el-option>
            <el-option :value="11" label=黑龙江></el-option>
            <el-option :value="12" label=辽宁></el-option>
            <el-option :value="13" label=河北></el-option>
            <el-option :value="14" label=河南></el-option>
            <el-option :value="15" label=山东></el-option>
            <el-option :value="16" label=江苏></el-option>
            <el-option :value="17" label=山西></el-option>
            <el-option :value="32" label=台湾></el-option>
            <el-option :value="4" label=港澳></el-option>
            <el-option :value="5" label=海外></el-option>
            <el-option :value="6" label=其他城市></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="负责人姓名" prop="enterpriseLeadName">
          <el-input v-model="form.enterpriseLeadName" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="负责人职务" prop="enterpriseLeadJob">
          <el-input v-model="form.enterpriseLeadJob" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="联系人姓名" prop="enterpriseContactName">
          <el-input v-model="form.enterpriseContactName" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="联系人职务" prop="enterpriseContactJob">
          <el-input v-model="form.enterpriseContactJob" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="联系人电话" prop="enterpriseContactPhone">
          <el-input v-model="form.enterpriseContactPhone" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="企业邮箱" prop="enterpriseEmail">
          <el-input v-model="form.enterpriseEmail" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="联系地址" prop="enterpriseAddress">
          <el-input v-model="form.enterpriseAddress" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="企业介绍" prop="enterpriseSummary">
          <el-input v-model="form.enterpriseSummary" type="textarea" :rows="3" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="办公环境" prop="officeImages">
          <el-upload ref="img" :show-file-list="false" :action="imgAction" :before-upload="beforeUpload"
            :on-success="uploadSuccess" list-type="picture-card" accept="image/*">
            <img v-if="form.officeImages" :src="form.officeImages | fullImgUrl('official')" width="148" height="148" style="object-fit: contain;" />
            <i v-else class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传不超过 <b style="color: red;">5MB</b> 的 <b style="color: red;">png/jpg/jpeg</b>文件，且不超过1个</div>
          </el-upload>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryList, getItem, delItem, addItem, updateItem } from "@/api/official/enterprise"
import { listLevel } from "@/api/mobile/level"

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isCheck: 0,
        isSubmit: 1,
        searchtxt: null,
      },
      levelList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      imgAction: `${process.env.VUE_APP_BASE_API}/common/ftpftpCommonUpload`,
    }
  },
  created() {
    listLevel(this.queryParams).then(response => {
      this.levelList = response.rows
    });
    this.getList()
  },
  methods: {
    getLevelFormat(status) {
      const dart = this.levelList.find(item => item.id === status);
      if (dart) {
        return dart.level;
      } else {
        return '未知';
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        avatar: null,
        nickname: null,
        realname: null,
        userLevel: null,
        cheese: null,
        integral: null,
        phone: null,
        lawyerJoblevel: null,
        isAdmin: null,
        sort: null,
        status: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = `修改信息`
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除【信息编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row, field='status') {
      let text = row[field] == 0 ? "停用" : "启用"
      if(field == 'isCheck') {
        text = "审核通过"
      }
      this.$modal.confirm(`确认要"${text}"该企业吗？`).then(function() {
        return updateItem({id :row.id, [field]: row[field]})
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row[field] = row[field] == "0" ? 1 : 0
      })
    },
    // 审核
    handleApprove(row) {
      this.$modal.confirm(`确认要"通过"该企业吗？`).then(function() {
        return updateItem({id :row.id, isCheck: 1})
      }).then(() => {
        this.$modal.msgSuccess("操作成功")
      })
    },
    beforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 5
      if (!isRightSize) {
        this.$message.error('文件大小超过 5MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    uploadSuccess(res){
      this.form.officeImages = res.path;
    }
  }
}
</script>

<style scoped>
.more-radio-group .el-radio{
  margin-top: 10px;
}
.other-input {
  margin-top: 8px;
}
</style>
